/*
 * NativeImplementationBlock.java  0.4.0 / Nov 6, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.tools.ng.delegate;

import java.util.ArrayList;

import com.thoughtworks.qdox.model.JavaClass;

import com.cute4j.tools.ng.AbstractNativeFile;
import com.cute4j.tools.ng.NativeFileText;
import com.cute4j.tools.ng.NativeGenerator;

/**
 * Native Implementation Block.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
class NativeImplementationBlock extends NativeFileText {

// Private Fields. -------------------------------------------------------------

  private final JavaClass javaClass;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new NativeDeclarationBlock object.
   *
   * @param nativeFile the abstract native file
   * @param text the text
   * @param javaClass the java class
   */
  public NativeImplementationBlock(AbstractNativeFile nativeFile, String text,
                                   JavaClass javaClass) {
    super(nativeFile, text);
    this.javaClass = javaClass;
  }

// Protected Methods. ----------------------------------------------------------

  /**
   * Returns JavaClass object.
   *
   * @return JavaClass object
   */
  protected JavaClass getJavaClass() {
    return javaClass;
  }

  /**
   * Returns method implementations form text.
   *
   * @return method implementations form text
   */
  protected ArrayList getMethodImplementationsFromText() {
    String text = getText();
    ArrayList list = new ArrayList();
    if (text != null) {
      int i = 0;
      int methodStart = 0;
      int state = 0;
      String newLine = NativeGenerator.getInstance().getNewLine();
      int newLineLength = newLine.length();
      char newLineLastChar = newLine.charAt(newLineLength - 1);
      int textLength = text.length();
      while (i < textLength) {
        char c = text.charAt(i);
        switch (state) {
          case 0:
            if (c == newLineLastChar) {
              state = 1;
            }
            break;
          case 1:
            if (c == '/') {
              state = 2;
              methodStart = i;
            } else {
              state = 0;
            }
            break;
          case 2:
            if (c == '*') {
              state = 3;
            } else {
              state = 0;
            }
            break;
          case 3:
            if (c == '!') {
              state = 4;
            } else {
              state = 0;
            }
            break;
          case 4:
            if (c == newLineLastChar) {
              state = 5;
            }
            break;
          case 5:
            if (c == '}') {
              state = 0;
              if (text.charAt(i + 1) == '*') {
                list.add(text.substring(methodStart, i + newLineLength + 3));
              } else {
                list.add(text.substring(methodStart, i + newLineLength + 1));
              }
            } else {
              state = 4;
            }
            break;
        }
        i++;
      }
    }
    return list;
  }

}
